package com.qztc.controller;

import com.qztc.hire_end_dto.dto.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;

import static com.qztc.config.GlobalConfig.SERVICE_URL;

@RestController
@RequestMapping("/consumer/admin")
@Tag(name = "消费者的管理员接口")
public class AdminControllerByRestTemplate {
    @Autowired
    private RestTemplate restTemplate;

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public String login(@RequestBody Admin admin) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Admin> httpEntity = new HttpEntity<>(admin, headers);
        ResponseEntity<String> response = restTemplate.exchange(SERVICE_URL+"/admin/login", HttpMethod.POST,httpEntity, String.class);
        return response.getBody();
    }

    @Operation(summary = "获取所有管理员")
    @GetMapping("/getAll")
    public String getAll() {
        return restTemplate.getForObject(SERVICE_URL+"/admin/getAll", String.class);
    }

    @Operation(summary = "删除管理员")
    @DeleteMapping("/delete")
    public String delete(@RequestBody Admin admin) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Admin> httpEntity = new HttpEntity<>(admin,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/admin/delete", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "添加管理员")
    @PostMapping("/add")
    public String add(@RequestBody Admin admin) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Admin> httpEntity = new HttpEntity<>(admin,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/admin/add", HttpMethod.POST,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "修改管理员")
    @PutMapping("/update")
    public String update(@RequestBody Admin admin) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Admin> httpEntity = new HttpEntity<>(admin,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/admin/update", HttpMethod.PUT,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "查询某一个管理员")
    @PostMapping("/getOneAdmin")
    public String getOneAdmin(@RequestBody Admin admin) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Admin> httpEntity = new HttpEntity<>(admin,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/admin/getOneAdmin", HttpMethod.GET, httpEntity,String.class);
        return result.getBody();
    }
    
    @Operation(summary = "批量删除管理员")
    @DeleteMapping("/deleteMany")
    public String deleteMany(@RequestBody List<Integer> adminIds) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<Integer>> httpEntity = new HttpEntity<>(adminIds,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/admin/deleteMany", HttpMethod.DELETE,httpEntity,String.class);
        return result.getBody();
    }

    @Operation(summary = "批量增加管理员")
    @PostMapping("/addMany")
    public String addMany(@RequestBody List<Admin> admins) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<Admin>> httpEntity = new HttpEntity<>(admins,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/admin/addMany", HttpMethod.POST,httpEntity,String.class);
        return result.getBody();
    }
}
